package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.MissileJD89;

public class MissileJD89Sql {

	private static MissileJD89Sql	instance					= null;
	private final String			TABLE_NAME					= "MissileJD89";

	private final String			KEY_MISSILEJD89_ID			= "id";
	private final String			KEY_MISSILEJD89_ANGEL		= "angel";			//角度
	private final String			KEY_MISSILEJD89_TIME		= "time";
	private final String			KEY_MISSILEJD89_HEIGHT		= "height";
	private final String			KEY_MISSILEJD89_LENGTH		= "length";
	private final String			KEY_MISSILEJD89_TOPHEIGHT	= "topheight";
	private final String			KEY_MISSILEJD89_TOPLENGTH	= "toplength";
	private final String			KEY_MISSILEJD89_RANGE		= "range";

	public static synchronized MissileJD89Sql getInstance() {
		if (null == instance) {
			instance = new MissileJD89Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_MISSILEJD89_ID + " INTEGER PRIMARY KEY," + KEY_MISSILEJD89_ANGEL + " INTEGER," + KEY_MISSILEJD89_TIME + " INTEGER," + KEY_MISSILEJD89_HEIGHT + " INTEGER," + KEY_MISSILEJD89_LENGTH + " INTEGER,"
					+ KEY_MISSILEJD89_TOPHEIGHT + " INTEGER," + KEY_MISSILEJD89_TOPLENGTH + " INTEGER," + KEY_MISSILEJD89_RANGE + " INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(MissileJD89 missile) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_MISSILEJD89_ANGEL, missile.angel);
			values.put(KEY_MISSILEJD89_TIME, missile.time);
			values.put(KEY_MISSILEJD89_HEIGHT, missile.height);
			values.put(KEY_MISSILEJD89_LENGTH, missile.length);
			values.put(KEY_MISSILEJD89_TOPHEIGHT, missile.topHeight);
			values.put(KEY_MISSILEJD89_TOPLENGTH, missile.topLength);
			values.put(KEY_MISSILEJD89_RANGE, missile.range);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(MissileJD89 missile) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_MISSILEJD89_ANGEL, missile.angel);
			values.put(KEY_MISSILEJD89_TIME, missile.time);
			values.put(KEY_MISSILEJD89_HEIGHT, missile.height);
			values.put(KEY_MISSILEJD89_LENGTH, missile.length);
			values.put(KEY_MISSILEJD89_TOPHEIGHT, missile.topHeight);
			values.put(KEY_MISSILEJD89_TOPLENGTH, missile.topLength);
			values.put(KEY_MISSILEJD89_RANGE, missile.range);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_MISSILEJD89_ID + " = ?", new String[] { String.valueOf(missile.id) });
		}
		return 0;
	}

	public void delete(MissileJD89 missile) {
		delete(missile.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_MISSILEJD89_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<MissileJD89> query() {
		ArrayList<MissileJD89> missiles = new ArrayList<MissileJD89>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_MISSILEJD89_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						MissileJD89 missile = new MissileJD89();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ANGEL));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	public ArrayList<MissileJD89> query(int time) {
		ArrayList<MissileJD89> missiles = new ArrayList<MissileJD89>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, KEY_MISSILEJD89_TIME + " = ? ", new String[] { String.valueOf(time) }, null, null, KEY_MISSILEJD89_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						MissileJD89 missile = new MissileJD89();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ANGEL));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	public ArrayList<MissileJD89> queryWithAngle(int angle) {
		ArrayList<MissileJD89> missiles = new ArrayList<MissileJD89>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, KEY_MISSILEJD89_ANGEL + " = ? ", new String[] { String.valueOf(angle) }, null, null, KEY_MISSILEJD89_TIME);
				if (cur != null && cur.moveToFirst()) {
					do {
						MissileJD89 missile = new MissileJD89();
						missile.id = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ID));
						missile.angel = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_ANGEL));
						missile.time = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TIME));
						missile.height = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_HEIGHT));
						missile.length = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_LENGTH));
						missile.topHeight = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPHEIGHT));
						missile.topLength = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_TOPLENGTH));
						missile.range = cur.getInt(cur.getColumnIndex(KEY_MISSILEJD89_RANGE));
						missiles.add(missile);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return missiles;
	}

	private void initData() {
		MissileJD89 missile = new MissileJD89();

		missile.angel = 45;
		missile.time = 8;
		missile.height = 2544;
		missile.length = 2770;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 10;
		missile.height = 2844;
		missile.length = 3195;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 12;
		missile.height = 3068;
		missile.length = 4594;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 14;
		missile.height = 3278;
		missile.range = 3974;
		insert(missile);

		missile.angel = 45;
		missile.time = 16;
		missile.height = 3422;
		missile.length = 4334;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.range = 10800;
		insert(missile);

		missile.angel = 45;
		missile.time = 18;
		missile.height = 3538;
		missile.length = 4678;
		missile.topHeight = 4250;
		missile.topLength = 6750;
		missile.length = 10800;
		insert(missile);

		missile.angel = 50;
		missile.time = 8;
		missile.height = 2739;
		missile.length = 2527;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 10;
		missile.height = 3071;
		missile.length = 2918;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 12;
		missile.height = 3345;
		missile.length = 3287;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 14;
		missile.height = 3566;
		missile.length = 3636;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 16;
		missile.height = 3738;
		missile.length = 3980;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 50;
		missile.time = 18;
		missile.height = 3866;
		missile.length = 4289;
		missile.topHeight = 4600;
		missile.topLength = 6100;
		missile.range = 10000;
		insert(missile);

		missile.angel = 55;
		missile.time = 8;
		missile.height = 3006;
		missile.length = 2203;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 10;
		missile.height = 3383;
		missile.length = 2553;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 12;
		missile.height = 3699;
		missile.length = 2878;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 14;
		missile.height = 3901;
		missile.length = 3186;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 16;
		missile.height = 4157;
		missile.length = 3483;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 55;
		missile.time = 18;
		missile.height = 4341;
		missile.length = 3767;
		missile.topHeight = 4950;
		missile.topLength = 5550;
		missile.range = 9150;
		insert(missile);

		missile.angel = 60;
		missile.time = 8;
		missile.height = 3204;
		missile.length = 1981;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 10;
		missile.height = 3613;
		missile.length = 2291;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 12;
		missile.height = 3961;
		missile.length = 2584;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 14;
		missile.height = 4225;
		missile.length = 2863;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 16;
		missile.height = 4438;
		missile.length = 3131;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 60;
		missile.time = 18;
		missile.height = 4694;
		missile.length = 3384;
		missile.topHeight = 5350;
		missile.topLength = 4900;
		missile.range = 8250;
		insert(missile);

		missile.angel = 65;
		missile.time = 8;
		missile.height = 3340;
		missile.length = 1678;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 10;
		missile.height = 3765;
		missile.length = 1952;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 12;
		missile.height = 4143;
		missile.length = 2192;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 14;
		missile.height = 4459;
		missile.length = 2431;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 16;
		missile.height = 4723;
		missile.length = 2619;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 65;
		missile.time = 18;
		missile.height = 4940;
		missile.length = 2881;
		missile.topHeight = 5800;
		missile.topLength = 4200;
		missile.range = 7300;
		insert(missile);

		missile.angel = 70;
		missile.time = 8;
		missile.height = 3513;
		missile.length = 1269;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 10;
		missile.height = 3975;
		missile.length = 1502;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 12;
		missile.height = 4375;
		missile.length = 1698;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 14;
		missile.height = 4718;
		missile.length = 1882;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 16;
		missile.height = 5010;
		missile.length = 2059;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 70;
		missile.time = 18;
		missile.height = 5203;
		missile.length = 2233;
		missile.topHeight = 6200;
		missile.topLength = 3500;
		missile.range = 6300;
		insert(missile);

		missile.angel = 75;
		missile.time = 8;
		missile.height = 3626;
		missile.length = 1296;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 10;
		missile.height = 4107;
		missile.length = 1196;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 12;
		missile.height = 4525;
		missile.length = 1351;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 14;
		missile.height = 4888;
		missile.length = 1500;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 16;
		missile.height = 5198;
		missile.length = 1623;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 75;
		missile.time = 18;
		missile.height = 5459;
		missile.length = 1781;
		missile.topHeight = 6600;
		missile.topLength = 2800;
		missile.range = 5200;
		insert(missile);

		missile.angel = 80;
		missile.time = 8;
		missile.height = 3691;
		missile.length = 1033;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 10;
		missile.height = 4184;
		missile.length = 804;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 12;
		missile.height = 4613;
		missile.length = 908;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 14;
		missile.height = 4987;
		missile.length = 1008;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 16;
		missile.height = 5307;
		missile.length = 1104;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 80;
		missile.time = 18;
		missile.height = 5597;
		missile.length = 1198;
		missile.topHeight = 7100;
		missile.topLength = 2100;
		missile.range = 3950;
		insert(missile);

		missile.angel = 85;
		missile.time = 8;
		missile.height = 3754;
		missile.length = 348;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 10;
		missile.height = 4257;
		missile.length = 403;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 12;
		missile.height = 4697;
		missile.length = 456;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 14;
		missile.height = 5080;
		missile.length = 507;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 16;
		missile.height = 5411;
		missile.length = 555;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);

		missile.angel = 85;
		missile.time = 18;
		missile.height = 5693;
		missile.length = 602;
		missile.topHeight = 7750;
		missile.topLength = 1200;
		missile.range = 2100;
		insert(missile);
	}
}
